/*
 * @Author: zhangfeng
 * @Date: 2023-12-08 16:02:02
 * @LastEditTime: 2024-01-17 18:09:02
 * @LastEditors: zhangfeng
 * @Description:
 * @FilePath: \manager\managersystem\src\store\index.ts
 * © 2023 张峰，All rights reserved.
 */

import { defineStore } from 'pinia';
import {
	generateRoutes,
	generateMenus,
	generatePremission,
	generateMenusShown,
} from '../utils/utils';
export const user = defineStore('mytoken', () => {
	// state
	const tokenJson = ref('');
	// getters
	const token = computed(() => {
		try {
			console.log(tokenJson.value);
			console.log(localStorage.getItem('saveToken'));
			return tokenJson.value || localStorage.getItem('saveToken');
		} catch {
			ElMessage.error('JSON 字符串格式不正确，转化失败');
		}
	});
	// action
	function saveToken(data: string) {
		console.log(data);
		tokenJson.value = data;
		localStorage.setItem('saveToken', data);
	}
	function removeToken() {
		tokenJson.value = '';
		console.log(tokenJson.value);
		localStorage.removeItem('saveToken');
	}

	return { token, saveToken, removeToken };
});

export const userInfo = defineStore('userInfo', () => {
	// state
	const uerInformation = ref({} as UserInfoMation);
	// getters
	const uerInfoName = computed(() => {
		return uerInformation.value.user_name;
	});
	const uerInfoRoleId = computed(() => {
		return uerInformation.value.roleIds;
	});
	const uerInfoRoleName = computed(() => {
		return uerInformation.value.roleName;
	});

	// action
	function saveInformation(data: UserInfoMation) {
		uerInformation.value = data;
	}

	return { uerInfoName, uerInfoRoleId, uerInfoRoleName, saveInformation };
});

export const menusList = defineStore('menusList', () => {
	const menus = ref([] as MenuItemTree[]);
	const routers = ref([] as premissionRouter[]);
	const prem = ref([] as string[]);
	const menusList = computed(() => {
		return menus.value;
	});
	const routersList = computed(() => {
		return routers.value;
	});

	const premis = computed(() => {
		return prem.value;
	});

	async function saveMenusList(data: MenuItem[]) {
		prem.value = await generatePremission(data);
		const res = await generateMenus(data);
		await generateRoutes(res);
		menus.value = await generateMenusShown(res);
		// routers.value = resRouter;
	}
	return { menusList, saveMenusList, routersList, premis };
});

// export const premission = defineStore('premission', () => {

// })
